package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import entity.GettingStartedSurvey;
import entity.Questions;
import entity.Score;
import entity.StrategicPlan;

import util.HibernateUtil;
import util.Util;
import vo.GettingStartedSurveyVO;
import vo.QuestionVO;

public class SurveyDAO {

	public List<GettingStartedSurveyVO> getSurveys(int strategicPlanId) {
		Session sessao = HibernateUtil.getSession();
		StrategicPlan sp = (StrategicPlan) sessao.load(StrategicPlan.class, strategicPlanId);
		
		List<GettingStartedSurveyVO> listaRetorno = new ArrayList<GettingStartedSurveyVO>();
		
		for(GettingStartedSurvey gss:sp.getListGettingStartedSurvey()){
			GettingStartedSurveyVO vo = new GettingStartedSurveyVO();
			vo.setData(gss.getData());
			vo.setId(gss.getId());
			vo.setScore(gss.getScore());
			vo.setDataString(Util.convertDateToString(gss.getData()));
			vo.setStrategicPlanId(strategicPlanId);
			
			List<QuestionVO> listaQuestion = new ArrayList<QuestionVO>();
			
			for(Score s:gss.getListaScore()){
				QuestionVO qvo = new QuestionVO();
				
				qvo.setQuestionId(s.getQuestion().getId());
				qvo.setQuestion(s.getQuestion().getQuestion());
				qvo.setGettingStartedSurveyId(s.getGettingStartedSurvey().getId());
				qvo.setScore(s.getScore());
				
				listaQuestion.add(qvo);
			}
			
			vo.setListaQuestion(listaQuestion);
			
			listaRetorno.add(vo);
		}
		
		sessao.close();
		return listaRetorno;
	}

	public void save(List<GettingStartedSurveyVO> listaSurvey) {
		GettingStartedSurvey gss;
		
		Session sessao = HibernateUtil.getSession();
		Transaction tx = sessao.beginTransaction();
		
		try{
			for(GettingStartedSurveyVO gssvo:listaSurvey){
				if (gssvo.getId() < 0){
					gss = new GettingStartedSurvey();
					gss.setData(gssvo.getData());
					gss.setScore(gssvo.getScore());
					gss.setListaScore(new ArrayList<Score>());
					
					StrategyDAO sDAO = new StrategyDAO();
					StrategicPlan sp = sDAO.getStrategicPlan(gssvo.getStrategicPlanId(), sessao);
					
					gss.setStrategicPlan(sp);
					
					for(QuestionVO qvo:gssvo.getListaQuestion()){
						Questions q = (Questions)sessao.load(Questions.class, qvo.getQuestionId());
						
						Score s = new Score();
						s.setScore(qvo.getScore());
						s.setGettingStartedSurvey(gss);
						s.setQuestion(q);
						
						gss.getListaScore().add(s);
					}
					
					sessao.saveOrUpdate(gss);
				}
			}
			
			tx.commit();
			sessao.close();
		}
		catch(Exception e){
			e.printStackTrace();
			tx.rollback();
			sessao.close();
		}
	}

	public List<QuestionVO> getQuestions() {
		Session sessao = HibernateUtil.getSession();
		Criteria criterios = sessao.createCriteria(Questions.class);
		
		List<?> lista = criterios.list();
		List<QuestionVO> retorno = new ArrayList<QuestionVO>();
		
		for(Object obj:lista){
			Questions q = (Questions) obj;
			QuestionVO qvo = new QuestionVO();
			
			qvo.setQuestionId(q.getId());
			qvo.setQuestion(q.getQuestion());
			
			retorno.add(qvo);
		}
		
		sessao.close();
		return retorno;
	}
}
